﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Class.Web.Mvc;
using Telerik.Web.Mvc;
using ThuySan.Bussiness;
using ThuySan.Models;

#endregion

namespace ThuySan.Areas.Backend.Controllers
{
    [Authorize]
    public class BannerController : BaseController
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(_unitOfWork.ImageRepository.Select(c => c.IsBanner == true).Select(c => new {
                                                                                                                          c.Id,
                                                                                                                          c.ImageName,
                                                                                                                          c.Priority,
                                                                                                                          IsBanner = c.IsBanner ?? false
                                                                                                                                             ? "checked"
                                                                                                                                             : "",
                                                                                                                          c.Description
                                                                                                                  })));
        }

        public ActionResult Add()
        {
            ViewBag.Products = _unitOfWork.ProductRepository.Select().Select(c => new {
                                                                                              c.ProductId,
                                                                                              c.Name
                                                                                      }).ToList().Select(c => new SelectListItem {
                                                                                                                                         Text = c.Name,
                                                                                                                                         Value = c.ProductId + ""
                                                                                                                                 });
            return View(new ImageModel());
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Add(ImageModel imageModel)
        {
            if (ModelState.IsValid)
            {
                Image image = imageModel;
                if (_unitOfWork.ImageRepository.CheckInsertImage(image))
                {
                    Exception exception;
                    image.IsBanner = true;
                    _unitOfWork.ImageRepository.Insert(image, out exception);
                    if (exception == null)
                    {
                        SaveResource("thumbnail", "Products", image);
                        image.ImageName = SaveBrushResource("thumbnail", "Products", image);
                        _unitOfWork.AcceptChanges();
                        return RedirectToAction("Index");
                    }
                }
            }
            ViewBag.Products = _unitOfWork.ProductRepository.Select().Select(c => new {
                                                                                              c.ProductId,
                                                                                              c.Name
                                                                                      }).ToList().Select(c => new SelectListItem {
                                                                                                                                         Text = c.Name,
                                                                                                                                         Value = c.ProductId + ""
                                                                                                                                 });
            return View(imageModel);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id, out guid))
            {
                Image image = _unitOfWork.ImageRepository.SelectbyId(guid);
                if (image != null)
                {
                    ViewBag.Products = _unitOfWork.ProductRepository.Select().Select(c => new {
                                                                                                      c.ProductId,
                                                                                                      c.Name
                                                                                              }).ToList().Select(c => new SelectListItem {
                                                                                                                                                 Text = c.Name,
                                                                                                                                                 Value = c.ProductId + ""
                                                                                                                                         });

                    ImageModel imageModel = image;
                    return View(imageModel);
                }
            }
            return Content("");
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Edit(ImageModel imageModel)
        {
            if (ModelState.IsValid)
            {
                Image image = _unitOfWork.ImageRepository.SelectbyId(imageModel.Id);
                imageModel.Update(image);
                if (_unitOfWork.ImageRepository.CheckUpdateImage(image))
                {
                    Exception exception;
                    image.IsBanner = true;
                    _unitOfWork.ImageRepository.Update(image, out exception);
                    if (exception == null)
                    {
                        SaveResource("thumbnail", "Products", image.ImageName, image);
                        image.ImageName = SaveBrushResource("thumbnail", "Products", image.ImageName, image);
                        _unitOfWork.AcceptChanges();
                        return RedirectToAction("Index");
                    }
                }
            }
            ViewBag.Products = _unitOfWork.ProductRepository.Select().Select(c => new {
                                                                                              c.ProductId,
                                                                                              c.Name
                                                                                      }).ToList().Select(c => new SelectListItem {
                                                                                                                                         Text = c.Name,
                                                                                                                                         Value = c.ProductId + ""
                                                                                                                                 });
            return View(imageModel);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s where int.TryParse(s1, out guid) select int.Parse(s1)).ToList();

                if (_unitOfWork.ImageRepository.CheckDeleteImage(list))
                {
                    _unitOfWork.ImageRepository.Delete(list, "Products", DeleteResource, DeleteBrushResource);
                }
            }
            return Json(new {
                                    err = 0,
                                    module = "Banner",
                                    msg = "Update Successfully"
                            }, JsonRequestBehavior.DenyGet);
        }
    }
}
